package polsl.biai.projekt.client;

import java.util.ArrayList;

import com.google.gwt.core.client.JsArray;

import com.google.gwt.maps.client.MapWidget;

import com.google.gwt.maps.client.services.DirectionsRenderer;
import com.google.gwt.maps.client.services.DirectionsRendererOptions;
import com.google.gwt.maps.client.services.DirectionsRequest;
import com.google.gwt.maps.client.services.DirectionsResult;
import com.google.gwt.maps.client.services.DirectionsResultHandler;
import com.google.gwt.maps.client.services.DirectionsService;
import com.google.gwt.maps.client.services.DirectionsStatus;
import com.google.gwt.maps.client.services.DirectionsWaypoint;
import com.google.gwt.maps.client.services.TravelMode;
import com.google.gwt.user.client.ui.Composite;

/**
 * shows cities and directions on map
 * due to Google Maps Api restrictions can show only up to 9 cities
 * @author Jacek
 *
 */
public class DirectionsServiceMapWidget extends Composite {

	private MapWidget mapWidget;
	private DirectionsRendererOptions options;
	private DirectionsRenderer directionsDisplay;

	private String travelMode = null;

	public DirectionsServiceMapWidget(MapWidget m) {
		mapWidget = m;
		options = DirectionsRendererOptions.newInstance();
		directionsDisplay = DirectionsRenderer.newInstance(options);

	}

	/**
	 * sets travelMode and starts function showing cities on map
	 * 
	 */
	public void addCitiesToMap(ArrayList<String> cities, String travelM) {
		travelMode = travelM;
		drawDirectionsWithMidPoint(cities);

	}

	/**
	 * shows cities and directions on map
	 */
	private void drawDirectionsWithMidPoint(ArrayList<String> cities) {

		// sets map on which show directions
		directionsDisplay.setMap(mapWidget);

		//sets origin and destination city
		String origin = cities.get(1);
		String destination = origin;

		//creates new DirectionsRequest
		DirectionsRequest request = DirectionsRequest.newInstance();
		//sets request's parameteres, like origin, destination, travelmode
		request.setOrigin(origin);
		request.setDestination(destination);
		request.setTravelMode(TravelMode.DRIVING);
		request.setOptimizeWaypoints(false);

		//creates waypoints
		JsArray<DirectionsWaypoint> waypoints = JsArray.createArray().cast();
		//adds cities t waypoints
		for (int i = 2; i < cities.size(); i++) {
			DirectionsWaypoint waypoint = DirectionsWaypoint.newInstance();
			waypoint.setStopOver(true);
			waypoint.setLocation(cities.get(i));
			waypoints.push(waypoint);
		}
		//adds waypoints to request
		request.setWaypoints(waypoints);

		//creates new DirectionsService
		DirectionsService directionsService = DirectionsService.newInstance();
		directionsService.route(request, new DirectionsResultHandler() {
			public void onCallback(DirectionsResult result,	DirectionsStatus status) {
				if (status == DirectionsStatus.OK) {
					//if request is ok cities and directions are shown on map
					directionsDisplay.setDirections(result);

				} else if (status == DirectionsStatus.INVALID_REQUEST) {
					System.out.println(status);

				} else if (status == DirectionsStatus.MAX_WAYPOINTS_EXCEEDED) {
					System.out.println(status);

				} else if (status == DirectionsStatus.NOT_FOUND) {
					System.out.println(status);

				} else if (status == DirectionsStatus.OVER_QUERY_LIMIT) {
					System.out.println(status);

				} else if (status == DirectionsStatus.REQUEST_DENIED) {
					System.out.println(status);

				} else if (status == DirectionsStatus.UNKNOWN_ERROR) {
					System.out.println(status);

				} else if (status == DirectionsStatus.ZERO_RESULTS) {
					System.out.println(status);

				}

			}
		});
	}

}
